const app = getApp();
import services from '../../services/clockin';
import moment from 'moment';
Page({
  data: {
    isReady: false,
    isLoading: false,
    unionid: '',
    mode: 0,
    modeOptions: [{
        title: '工地维度'
      },
      {
        title: '员工维度'
      }
    ],
    formData1: {
      station_id: '', // 工地
      shift_type: 0, // 班次：0白班 1夜班
      statistic_day: '' // 日期
    },
    formData2: {
      statistic_day: '' // 日期
    },
    stationList: [],
    shiftTypeList: [{
        label: '白班',
        value: 0
      },
      {
        label: '夜班',
        value: 1
      }
    ],
    tableData: []
  },
  async onLoad() {
    const {
      unionid
    } = app.globalData.userInfo || {};
    this.setData({
      unionid: unionid || '2QV8HeciS9zgYhRAtxbpiPZwiEiE'
    });
    await this.initData();
    this.setData({
      isReady: true
    });
  },
  async onShow() {
    if (!this.data.isReady) {
      await this.initData();
    }
  },
  async initData() {
    await this.queryGetStation();
    this.handleSearch();
  },
  async queryGetStation() {
    try {
      const res = await services.getStation(this.data.unionid);
      if (res && res.data && res.data.length) {
        const stationList = res.data.map((item) => {
          const {
            name,
            id
          } = item || {};
          return {
            label: name,
            value: id
          };
        });
        this.setData({
          stationList,
          formData1: {
            ...this.data.formData1,
            station_id: stationList[0].value,
            statistic_day: moment()
          },
          formData2: {
            ...this.data.formData2,
            statistic_day: moment()
          }
        });
        console.log('stationList', this.data);
      }
    } catch (error) {
      console.log('获取补卡列表失败', error);
    }
  },
  handleSearch() {
    if (this.data.mode === 0) {
      this.queryGetReportStationDaily();
    } else {
      this.queryGetReportEmployeeDaily();
    }
  },
  handleChangeMode(current) {
    this.setData({
      mode: current || 0
    });
    switch (current) {
      case 0: {
        this.setData({
          tableData: []
        });
        this.queryGetReportStationDaily();
        break;
      }
      case 1: {
        this.setData({
          tableData: []
        });
        this.queryGetReportEmployeeDaily();
        break;
      }
    }
  },
  handleChangeStationId(value) {
    console.log('handleChangeStationId', value);
    this.setData({
      formData1: {
        ...this.data.formData1,
        station_id: value
      }
    });
  },
  handleChangeShiftType(value) {
    console.log('handleChangeShiftType', value);
    this.setData({
      formData1: {
        ...this.data.formData1,
        shift_type: value
      }
    });
  },
  handleChangeDay(value) {
    console.log('handleChangeDay', value);
    if (this.data.mode === 0) {
      this.setData({
        formData1: {
          ...this.data.formData1,
          statistic_day: value
        }
      });
    } else {
      this.setData({
        formData2: {
          ...this.data.formData2,
          statistic_day: value
        }
      });
    }
  },
  async queryGetReportEmployeeDaily() {
    const {
      statistic_day
    } = this.data.formData2;
    console.log('queryGetReportStationDaily', statistic_day);
    if (!statistic_day) {
      dd.showToast({
        type: 'info',
        content: '请选择日期',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        statistic_day: moment(statistic_day).format('YYYYMMDD') // 日期
      };
      const res = await services.getReportEmployeeDaily(params, this.data.unionid);
      console.log('queryGetReportStationDaily', this.formData1, params, res);
      if (res && res.data) {
        this.setData({
          tableData: [...res.data]
        });
        console.log('tableData', this.tableData);
      }
    } catch (error) {
      console.log('error', error);
      this.setData({
        tableData: []
      });
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
  async queryGetReportStationDaily() {
    const {
      station_id,
      shift_type,
      statistic_day
    } = this.data.formData1;
    console.log('queryGetReportStationDaily', station_id, shift_type, statistic_day);
    if (!station_id) {
      dd.showToast({
        type: 'info',
        content: '请选择工地',
        duration: 1000
      });
      return;
    }
    if (!shift_type && shift_type !== 0) {
      dd.showToast({
        type: 'info',
        content: '请选择班次',
        duration: 1000
      });
      return;
    }
    if (!statistic_day) {
      dd.showToast({
        type: 'info',
        content: '请选择日期',
        duration: 1000
      });
      return;
    }
    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    });
    try {
      const params = {
        station_id, // 工地
        shift_type, // 班次：0白班 1夜班
        statistic_day: moment(statistic_day).format('YYYYMMDD') // 日期
      };
      const res = await services.getReportStationDaily(params, this.data.unionid);
      console.log('queryGetReportStationDaily', this.formData1, params, res);
      if (res && res.data) {
        this.setData({
          tableData: [...res.data]
        });
        console.log('tableData', this.tableData);
      }
    } catch (error) {
      console.log('error', error);
      this.setData({
        tableData: []
      });
    } finally {
      this.setData({
        isLoading: false
      });
    }
  },
  handleClickName(e) {
    const {
      info = {}
    } = e.currentTarget.dataset || {}; // 打卡类型 0: 上班打卡 1: 下班打卡
    const {
      id,
      name
    } = info.user || {}
    const statistic_day = moment(this.data.formData2.statistic_day).format('YYYYMMDD')
    console.log(id, name, statistic_day, this.data.formData2)
    dd.navigateTo({
      url: `/page/reportDailyUser/reportDailyUser?id=${id}&name=${name}&statistic_day=${statistic_day}`
    });
  }
});